package cn.edu.neusoft.food229.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.view.menu.ActionMenuItemView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.squareup.picasso.Picasso;

import java.util.List;

import cn.edu.neusoft.food229.Commen.Commen;
import cn.edu.neusoft.food229.R;
import cn.edu.neusoft.food229.adapter.FoodAdapter;
import cn.edu.neusoft.food229.bean.FoodBean;
import cn.edu.neusoft.food229.bean.IsCollectBean;
import cn.edu.neusoft.food229.bean.Result;
import cn.edu.neusoft.food229.bean.ShopBean;
import cn.edu.neusoft.food229.listener.CollectListener;
import cn.edu.neusoft.food229.listener.FoodListener;
import cn.edu.neusoft.food229.listener.ShopDetailListener;
import cn.edu.neusoft.food229.listener.isCollectListener;
import cn.edu.neusoft.food229.model.CollectModel;
import cn.edu.neusoft.food229.model.Foodmodel;

public class ShopActivity extends AppCompatActivity {
    private FoodAdapter adapter;
    private LinearLayoutManager layoutManager;
    private TextView tv_name,tv_tel,tv_pro,tv_address;
    private ImageView img;
    private ImageButton shop_collect;
    private RecyclerView recyclerView;
    private String shop_id;
    Context context;
    private int flag=0;
    CollectModel collectmodel=new CollectModel();
    private SharedPreferences sp;//简单存储
    private String User_id;
    isCollectListener listener=new isCollectListener() {

        @Override
        public void onResponce(IsCollectBean bean, String user_id, String shop_food_id, int flag) {
            if(bean.getCollected().equals("0")){
                System.out.println("---未收藏_店铺"+bean.getCollected());
                shop_collect.setImageDrawable(getResources().getDrawable(R.drawable.ic_collect1));
            }
            else{
                System.out.println("==已收藏=店铺=^_^"+bean.getCollected());
                shop_collect.setImageDrawable(getResources().getDrawable(R.drawable.ic_collect));
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop);
        context=ShopActivity.this;
        shop_id=getIntent().getStringExtra("shop_id");
        sp=getSharedPreferences("login",MODE_PRIVATE);
        User_id=sp.getString("userid","");

        initViews();


    }

    private void initViews() {
        img=findViewById(R.id.imageView10);
        tv_name=findViewById(R.id.textView16);
        tv_address=findViewById(R.id.textView17);
        tv_pro=findViewById(R.id.textView18);
        tv_tel=findViewById(R.id.textView19);
        recyclerView=findViewById(R.id.recyclerView2);
        shop_collect=findViewById(R.id.shop_collect);
        shop_collect.getBackground().setAlpha(1);

        layoutManager=new LinearLayoutManager(context);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);//固定条目高度，提高性能

        shop_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                collectmodel.collectShop(User_id, shop_id, new CollectListener() {
                    @Override
                    public void onResponce(Result result, String user_id, String shopid) {
                        if(result.getSuccess().equals("0")){
                            System.out.println("----尚未收藏"+result.getSuccess());
                        }else{

                            collectmodel.collect(User_id,shop_id,flag,listener);
                            System.out.println("---收藏"+result.getSuccess());
                        }
                    }

                    @Override
                    public void onFail(String msg) {
                        System.out.println("---fail");
                    }
                });
            }
        });
        collectmodel.collect(User_id,shop_id,flag,listener);

        Foodmodel model=new Foodmodel();
        model.getResult(shop_id, new FoodListener() {
            @Override
            public void onResponse(List<FoodBean> beanList) {
                adapter=new FoodAdapter(context,beanList);
                recyclerView.setAdapter(adapter);
            }

            @Override
            public void onFail(String msg) {

            }
        });
        model.getResultShopList(shop_id, new ShopDetailListener() {
            @Override
            public void onSuccess(ShopBean bean, String shop_id) {
                Picasso.with(context)
                        .load(Commen.BASEURL+bean.getPic())
                        .placeholder(R.mipmap.ic_launcher)
                        .into(img);
                tv_name.setText(bean.getShopname());
                tv_address.setText(bean.getAddress());
                tv_pro.setText(bean.getIntro());
                tv_tel.setText(bean.getPhonenum());
            }

            @Override
            public void onFail(String msg) {

            }
        });
        layoutManager=new LinearLayoutManager(context);
        recyclerView.setAdapter(adapter);

    }

}
